<template>
  <ContentWrap title="角色列表">
    <template #extr>
      <BaseButton type="success" @click="addRole">添加角色</BaseButton>
    </template>
    <el-row>
      <el-form :inline="true" :model="queryParam">
        <el-form-item label="角色名">
          <el-input v-model="queryParam.QueryKey" placeholder="角色名" @change="load" />
        </el-form-item>
        <el-form-item>
          <BaseButton @click="reset">重置</BaseButton>
        </el-form-item>
      </el-form>
    </el-row>
    <Table :data="roles" :columns="columns" :pagination="paging" @change="load">
      <template #IsEnable="e">
        <el-switch
          v-model="e.row.IsEnable"
          :disabled="e.row.IsDefRole"
          :inactive-value="false"
          :active-value="true"
          @change="(val) => setIsEnable(e.row, val)"
        />
      </template>
      <template #IsAdmin="e">
        <el-switch
          :disabled="e.row.IsEnable"
          v-model="e.row.IsAdmin"
          :inactive-value="false"
          :active-value="true"
          @change="(val) => setIsAdmin(e.row, val)"
        />
      </template>
      <template #EmpNum="e">
        <el-button type="text" @click="showEmp(e.row)">{{ e.row.EmpNum }}</el-button>
      </template>
      <template #IsDefRole="e">
        <el-switch
          v-model="e.row.IsDefRole"
          :disabled="e.row.IsEnable == false"
          :inactive-value="false"
          :active-value="true"
          @change="(val) => setIsDefRole(e.row, val)"
        />
      </template>
      <template #AddTime="e">
        {{ $formatTime(e.row.AddTime, 'yyyy-MM-dd HH:mm') }}
      </template>
      <template #action="e">
        <BaseButton
          size="small"
          type="primary"
          title="显示"
          :icon="viewIcon"
          circle
          @click="showView(e.row)"
        />
        <BaseButton
          v-if="e.row.IsEnable == false"
          size="small"
          type="primary"
          title="编辑角色"
          :icon="editIcon"
          circle
          @click="edit(e.row)"
        />
        <BaseButton
          v-if="e.row.IsEnable == false"
          size="small"
          type="danger"
          title="删除角色"
          :icon="dropIcon"
          circle
          @click="dropRole(e.row)"
        />
      </template>
    </Table>
    <roleView v-model:visible="viewVisable" :role-id="roleId" :comId="comId" />
    <editRole v-model:visible="visable" :role-id="roleId" :comId="comId" />
  </ContentWrap>
</template>

<script>
import * as roleApi from '@/api/role/role'
import { Table } from '@/components/Table'
import { useUserStoreWithOut } from '@/store/modules/user'
import { useIcon } from '@/hooks/web/useIcon'
import roleView from './components/roleView.vue'
import editRole from './components/editRole.vue'
const userStore = useUserStoreWithOut()
export default {
  components: {
    Table,
    roleView,
    editRole
  },
  data() {
    return {
      dropIcon: useIcon({ icon: 'ep:delete' }),
      editIcon: useIcon({ icon: 'ep:edit' }),
      viewIcon: useIcon({ icon: 'ep:view' }),
      roles: [],
      queryParam: {
        QueryKey: null
      },
      visable: false,
      viewVisable: false,
      roleId: null,
      columns: [
        {
          sortby: 'RoleName',
          field: 'RoleName',
          label: '角色名',
          align: 'center',
          minWidth: 150,
          sortable: 'custom'
        },
        {
          sortby: 'Remark',
          field: 'Remark',
          label: '备注',
          align: 'center',
          minWidth: 200,
          sortable: 'custom'
        },
        {
          sortby: 'IsDefRole',
          field: 'IsDefRole',
          label: '默认角色',
          align: 'center',
          slotName: 'IsDefRole',
          minWidth: 100,
          sortable: 'custom'
        },
        {
          sortby: 'IsAdmin',
          field: 'IsAdmin',
          label: '是否为管理员',
          align: 'center',
          slotName: 'IsAdmin',
          minWidth: 100,
          sortable: 'custom'
        },
        {
          sortby: 'IsEnable',
          field: 'IsEnable',
          label: '是否启用',
          align: 'center',
          minWidth: 100,
          slotName: 'IsEnable',
          sortable: 'custom'
        },
        {
          field: 'EmpNum',
          label: '员工数',
          align: 'right',
          minWidth: 100,
          slotName: 'EmpNum'
        },
        {
          field: 'AddTime',
          label: '添加时间',
          align: 'center',
          minWidth: 120,
          slotName: 'AddTime'
        },
        {
          field: 'Action',
          label: '操作',
          align: 'left',
          fixed: 'right',
          width: '150px',
          slotName: 'action'
        }
      ],
      paging: {
        pageSize: 10,
        total: 0,
        currentPage: 1
      }
    }
  },
  computed: {
    comId() {
      return userStore.comId
    }
  },
  mounted() {
    this.reset()
  },
  methods: {
    showEmp(row) {
      this.$router.push({ name: 'user', query: { roleId: row.Id } })
    },
    addRole() {
      this.roleId = null
      this.visable = true
    },
    edit(row) {
      this.roleId = row.Id
      this.visable = true
    },
    close(isRefresh) {
      if (isRefresh) {
        this.reset()
      }
    },
    showView(row) {
      this.roleId = row.Id
      this.viewVisable = true
    },
    async setIsEnable(row, isEnable) {
      await roleApi.setIsEnable(row.Id, isEnable)
      row.IsEnable = isEnable
    },
    async setIsAdmin(row, isAdmin) {
      await roleApi.setIsAdmin(row.Id, isAdmin)
      row.IsAdmin = isAdmin
    },
    async setIsDefRole(row, isDef) {
      if (!isDef) {
        this.$message({
          type: 'error',
          message: '必须存在一个默认角色!'
        })
        return
      }
      await roleApi.setIsDefRole(row.Id)
      const def = this.roles.find((c) => c.IsDefRole)
      def.IsDefRole = !isDef
      row.IsDefRole = isDef
    },
    dropRole(row) {
      const title = '确认删除角色 ' + row.RoleName + '?'
      this.$confirm(title, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.submitDrop(row.Id)
      })
    },
    async submitDrop(id) {
      await roleApi.deleteRole(id)
      this.$message({
        type: 'success',
        message: '删除成功!'
      })
      this.load()
    },
    reset() {
      this.queryParam.CompanyId = this.comId
      this.queryParam.QueryKey = null
      this.load()
    },
    async load(curPaging) {
      if (curPaging == null) {
        curPaging = {
          Size: this.paging.pageSize,
          Index: 1,
          SortName: 'Id',
          IsDesc: true
        }
        this.paging.currentPage = 1
      }
      const res = await roleApi.query(this.queryParam, curPaging)
      this.roles = res.List
      this.paging.total = res.Count
    }
  }
}
</script>
